-- Author:JnJ
-- Name:paintByNumbersJnJ
-- Description: First parameter is the detail layer id. Combined layers are in the range [numLayers, numLayers+numCombinedLayers). Second parameter is half the width in meters
-- Icon:
-- Hide:no


--[[
    paintTerrainBySpline ( take x,z value from spline every 0.5 meter and paint selected texture on the terrain)
	To use, enter the relevant texture number into the following locations
	Line 39 local offsetTexture = false - allows multiple textures when set to true default is false
	Line 40 local texLeft = Lefthand texture,
	Line 41 local texCentre = Centre texture
	Line 42 local texRight = RightHand texture, 
	Line 43 local numTex = Total Number of Textures in the Map-- needed to stop GE crashing if higher number entered
	Chose width and distance and enter those in the relevant location
	Line 44 local mSideCount = half the width in metres
	Line local texCentreWidth = mSideCount  adjusts the width of centre texture by varying the figure default 6
	Line 49 local mSplinePiece =  how far apart the textures are along the length of the spline (0.5 default)
	
	Select spline and execute script. WARNING texture cannot be undone but can be replaced by redoing with original texture
	Width of centre texture can now be set seperately but other textures limited to full side width minus the centre width
--]]


function crossProduct(ax, ay, az, bx, by, bz)
    return ay*bz - az*by, az*bx - ax*bz, ax*by - ay*bx;
end


		local mSplineID = getSelection(0)
		if mSplineID == 0 and getName(mSplineID) ~= "spline" and "spline01" then
    print("ERROR: Please select 'spline!")
		return
end
		local worldRoot = getRootNode()
		local map = getChildAt(worldRoot,0)
		local mTerrainID = getChild(map, "terrain")
		local offsetTexture = false --- when set to true allows multiple textures and widths
		local texRight = 75  ---lefthand texture No,
		local texCentre = 22 ---centre texture No,
		local texLeft = 75 ---righthand texture No,
		local numTex = 120     ---Total Number of Textures in the Map
        local mSideCount = 5 ---half the width in meters
		local texCentreWidth = mSideCount - 5 ---width of centre texture
		if texCentreWidth < 0 then 
        print("Error texCentreWidth less than 0")
		return 
        end;
        local mSplinePiece = 0.2 -- real size 0.5 meter, Distance textures are apart along the length of the spline
		
        if texCentre and texLeft and  texRight  > numTex   then
	print("ERROR in Texture Number")	
		end;

        local mSplineLength = getSplineLength( mSplineID ) ;
        local mSplinePiecePoint = mSplinePiece / mSplineLength ; -- relative size [0..1]
        local mSplinePos = 0.0;
        while mSplinePos <= 1.0 do
            -- get XYZ at position on spline
            local mPosX, mPosY, mPosZ = getSplinePosition( mSplineID, mSplinePos );
            -- directional vector at the point
            local mDirX, mDirY, mDirZ   = worldDirectionToLocal( mSplineID, getSplineDirection ( mSplineID, mSplinePos) );
            local mVecDx, mVecDy, mVecDz = crossProduct( mDirX, mDirY, mDirZ, 0, 1, 0);
            -- paint at the center
			if offsetTexture then
			for i = 0, texCentreWidth, 1 do
             local mNewPosX1 = mPosX + i * mVecDx;
                local mNewPosZ1 = mPosZ + i * mVecDz;
                local mNewPosX2 = mPosX  - i * mVecDx;
                local mNewPosZ2 = mPosZ  - i * mVecDz;
            setTerrainLayerAtWorldPos(mTerrainID, texCentre,mNewPosX1, mPosY, mNewPosZ1, 128.0 );
            setTerrainLayerAtWorldPos(mTerrainID, texCentre, mNewPosX2, mPosY, mNewPosZ2, 128.0 );
            end
			else
			texCentreWidth = 1
			setTerrainLayerAtWorldPos(mTerrainID, texCentre, mPosX , mPosY, mPosZ, 128.0 );
			end
            -- define side to side shift in meters
            for i = texCentreWidth, mSideCount, 1 do
                local mNewPosX1 = mPosX + i * mVecDx;
                local mNewPosZ1 = mPosZ + i * mVecDz;
                local mNewPosX2 = mPosX  - i * mVecDx;
                local mNewPosZ2 = mPosZ  - i * mVecDz;
                -- paint textures
			if not offsetTexture then
				texLeft = texCentre
				texRight = texCentre
			end
                setTerrainLayerAtWorldPos(mTerrainID, texRight, mNewPosX1, mPosY, mNewPosZ1, 128.0 );
                setTerrainLayerAtWorldPos(mTerrainID, texLeft, mNewPosX2, mPosY, mNewPosZ2, 128.0 );
            end
            -- goto next point
            mSplinePos = mSplinePos + mSplinePiecePoint;
        end
    
print(string.format("Painted:texLeft %s, texCentre %s texRight, %s", texLeft, texRight, texCentre))